home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / ireclaim.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  5.4 KB  |  185 lines

  1. /* Copyright (C) 1995, 1996, 1997, 1998, 1999 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19. /*$Id: ireclaim.c,v 1.2 2000/09/19 19:00:46 lpd Exp $ */
  20. /* Interpreter's interface to garbage collector */
  21. #include "ghost.h"
  22. #include "errors.h"
  23. #include "gsstruct.h"
  24. #include "iastate.h"
  25. #include "icontext.h"
  26. #include "interp.h"
  27. #include "isave.h"        /* for isstate.h */
  28. #include "isstate.h"        /* for mem->saved->state */
  29. #include "dstack.h"        /* for dsbot, dsp, dict_set_top */
  30. #include "estack.h"        /* for esbot, esp */
  31. #include "ostack.h"        /* for osbot, osp */
  32. #include "opdef.h"        /* for defining init procedure */
  33. #include "store.h"        /* for make_array */
  34.  
  35. /* Import preparation and cleanup routines. */
  36. extern void ialloc_gc_prepare(P1(gs_ref_memory_t *));
  37.  
  38. /* Forward references */
  39. private void gs_vmreclaim(P2(gs_dual_memory_t *, bool));
  40.  
  41. /* Initialize the GC hook in the allocator. */
  42. private int ireclaim(P2(gs_dual_memory_t *, int));
  43. private int
  44. ireclaim_init(i_ctx_t *i_ctx_p)
  45. {
  46.     gs_imemory.reclaim = ireclaim;
  47.     return 0;
  48. }
  49.  
  50. /* GC hook called when the allocator returns a VMerror (space = -1), */
  51. /* or for vmreclaim (space = the space to collect). */
  52. private int
  53. ireclaim(gs_dual_memory_t * dmem, int space)
  54. {
  55.     bool global;
  56.     gs_ref_memory_t *mem;
  57.  
  58.     if (space < 0) {
  59.     /* Determine which allocator got the VMerror. */
  60.     gs_memory_status_t stats;
  61.     ulong allocated;
  62.     int i;
  63.  
  64.     mem = dmem->space_global;    /* just in case */
  65.     for (i = 0; i < countof(dmem->spaces_indexed); ++i) {
  66.         mem = dmem->spaces_indexed[i];
  67.         if (mem == 0)
  68.         continue;
  69.         if (mem->gc_status.requested > 0 ||
  70.         ((gs_ref_memory_t *)mem->stable_memory)->gc_status.requested > 0
  71.         )
  72.         break;
  73.     }
  74.     gs_memory_status((gs_memory_t *) mem, &stats);
  75.     allocated = stats.allocated;
  76.     if (mem->stable_memory != (gs_memory_t *)mem) {
  77.         gs_memory_status(mem->stable_memory, &stats);
  78.         allocated += stats.allocated;
  79.     }
  80.     if (allocated >= mem->gc_status.max_vm) {
  81.         /* We can't satisfy this request within max_vm. */
  82.         return_error(e_VMerror);
  83.     }
  84.     } else {
  85.     mem = dmem->spaces_indexed[space >> r_space_shift];
  86.     }
  87.     if_debug3('0', "[0]GC called, space=%d, requestor=%d, requested=%ld\n",
  88.           space, mem->space, (long)mem->gc_status.requested);
  89.     global = mem->space != avm_local;
  90.     /* Since dmem may move, reset the request now. */
  91.     ialloc_reset_requested(dmem);
  92.     gs_vmreclaim(dmem, global);
  93.  
  94.     ialloc_set_limit(mem);
  95.     return 0;
  96. }
  97.  
  98. /* Interpreter entry to garbage collector. */
  99. private void
  100. gs_vmreclaim(gs_dual_memory_t *dmem, bool global)
  101. {
  102.     /* HACK: we know the gs_dual_memory_t is embedded in a context state. */
  103.     i_ctx_t *i_ctx_p =
  104.     (i_ctx_t *)((char *)dmem - offset_of(i_ctx_t, memory));
  105.     gs_ref_memory_t *lmem = dmem->space_local;
  106.     int code = context_state_store(i_ctx_p);
  107.     gs_ref_memory_t *memories[5];
  108.     gs_ref_memory_t *mem;
  109.     int nmem, i;
  110.  
  111.     memories[0] = dmem->space_system;
  112.     memories[1] = mem = dmem->space_global;
  113.     nmem = 2;
  114.     if (lmem != dmem->space_global)
  115.     memories[nmem++] = lmem;
  116.     for (i = nmem; --i >= 0;) {
  117.     mem = memories[i];
  118.     if (mem->stable_memory != (gs_memory_t *)mem)
  119.         memories[nmem++] = (gs_ref_memory_t *)mem->stable_memory;
  120.     }
  121.  
  122.     /****** ABORT IF code < 0 ******/
  123.     for (i = nmem; --i >= 0; )
  124.     alloc_close_chunk(memories[i]);
  125.  
  126.     /* Prune the file list so it won't retain potentially collectible */
  127.     /* files. */
  128.  
  129.     for (i = (global ? i_vm_system : i_vm_local);
  130.      i < countof(dmem->spaces_indexed);
  131.      ++i
  132.      ) {
  133.     gs_ref_memory_t *mem = dmem->spaces_indexed[i];
  134.  
  135.     if (mem == 0 || (i > 0 && mem == dmem->spaces_indexed[i - 1]))
  136.         continue;
  137.     if (mem->stable_memory != (gs_memory_t *)mem)
  138.         ialloc_gc_prepare((gs_ref_memory_t *)mem->stable_memory);
  139.     for (;; mem = &mem->saved->state) {
  140.         ialloc_gc_prepare(mem);
  141.         if (mem->saved == 0)
  142.         break;
  143.     }
  144.     }
  145.  
  146.     /* Do the actual collection. */
  147.  
  148.     {
  149.     void *ctxp = i_ctx_p;
  150.     gs_gc_root_t context_root;
  151.  
  152.     gs_register_struct_root((gs_memory_t *)lmem, &context_root,
  153.                 &ctxp, "i_ctx_p root");
  154.     GS_RECLAIM(&dmem->spaces, global);
  155.     gs_unregister_root((gs_memory_t *)lmem, &context_root, "i_ctx_p root");
  156.     i_ctx_p = ctxp;
  157.     dmem = &i_ctx_p->memory;
  158.     }
  159.  
  160.     /* Update caches not handled by context_state_load. */
  161.  
  162.     *systemdict = *ref_stack_index(&d_stack, ref_stack_count(&d_stack) - 1);
  163.  
  164.     /* Reload the context state. */
  165.  
  166.     code = context_state_load(i_ctx_p);
  167.     /****** ABORT IF code < 0 ******/
  168.  
  169.     /* Update the cached value pointers in names. */
  170.  
  171.     dicts_gc_cleanup();
  172.  
  173.     /* Reopen the active chunks. */
  174.  
  175.     for (i = 0; i < nmem; ++i)
  176.     alloc_open_chunk(memories[i]);
  177. }
  178.  
  179. /* ------ Initialization procedure ------ */
  180.  
  181. const op_def ireclaim_l2_op_defs[] =
  182. {
  183.     op_def_end(ireclaim_init)
  184. };
  185.